package univ.bme.accounting.dao;

import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

import univ.bme.accounting.domain.Category;

@Stateless
public class CategoryDAOImpl extends GenericDAOImpl<Category> implements CategoryDAO {

    public CategoryDAOImpl() {
    	super(Category.class);
    }
    
    public void delete(Category category) {
        super.delete(category.getId(), Category.class);
    }
    
    public List<Category> findCategoriesForUser(int user_id) {
    	 CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
         CriteriaQuery<Category> criteriaQuery = criteriaBuilder.createQuery(Category.class);
         Root<Category> entityRoot = criteriaQuery.from(Category.class);
         criteriaQuery.where(criteriaBuilder.equal(entityRoot.get("user"), user_id));
         criteriaQuery.select(entityRoot);
         List<Category> c = em.createQuery(criteriaQuery).getResultList();
         return c;
    }
}